package com.car.ljc.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.car.ljc.type.Constants;
import com.car.ljc.util.CommonUtil;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 停车场
 * @Author ljc
 * @Date 2018/9/20 14:48
 */
public class PakingLot {
    private String token;//用户token
    private List<Car> listCar = new ArrayList<>();//车list

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public List<Car> getListCar() {
        return listCar;
    }

    public void setListCar(List<Car> listCar) {
        this.listCar = listCar;
    }
    //初始化数据
    public boolean init(){
        if(!StringUtils.isEmpty(token)){
            CommonUtil.get(Constants.INIT_STATE_URL + CommonUtil.buildTokenAndTime(this.token));
            String strPos = CommonUtil.get(Constants.INIT_POS_URL + CommonUtil.buildTokenAndTime(this.token));
            CommonUtil.get(Constants.INIT_STATE_URL_END + CommonUtil.buildTokenAndTime(this.token));
            JSONObject jsonObject = JSON.parseObject(strPos);
            JSONArray datas = JSONArray.parseArray(jsonObject.get("data").toString());
            Object objList = datas.get(5);
            JSONArray objects = JSONArray.parseArray(objList.toString());
            for(int i=0;i<objects.size();i++){
                Car car = new Car();
                car.setPos(i);
                JSONObject carJson = JSON.parseObject(objects.get(i).toString());
                car.setLevel(CommonUtil.obj2Int(carJson.get("id")));
                int state = CommonUtil.obj2Int(carJson.get("state"));
                if(state == Constants.CAR_STATE_0 && car.getLevel() > 0) {
                    openBox(car);
                }else if(state == Constants.CAR_STATE_1){
                    changeState(car,Constants.CAR_STATE_2);
                }
                car.setState(CommonUtil.obj2Int(carJson.get("state")));
                if(car.getLevel() > 0){
                    this.listCar.add(car);
                }
            }
            return true;
        }
        return false;
    }
    //同步数据
    public void syns(){
        String strSyns = CommonUtil.get(Constants.SYNC_DATA_URL + CommonUtil.buildTokenAndTime(this.token));
        JSONObject jsonObject = JSON.parseObject(strSyns);
        System.out.println("syns:"+strSyns);
        Object dropBox = jsonObject.get("dropBox");

        if(dropBox != null){
            Car car = new Car();
            JSONObject jsonDropBox = JSON.parseObject(dropBox.toString());
            car.setPos(CommonUtil.obj2Int(jsonDropBox.get("pos")));
            JSONObject carJson = JSONObject.parseObject(jsonDropBox.get("car").toString());
            car.setLevel(CommonUtil.obj2Int(carJson.get("id")));
            car.setState(Constants.CAR_STATE_0);
            //开箱
            openBox(car);
        }
    }
    //开箱方法
    private void openBox(Car car){
        String s = CommonUtil.get(Constants.OPEN_BOX_URL + CommonUtil.buildTokenAndTime(this.token) + "&pos=" + car.getPos());
        JSONObject jsonObject = JSON.parseObject(s);
        System.out.println("openBox----:"+s);
        //开箱成功
        if(jsonObject.get("getCar") != null){
            Car one = canHe(car);
            if(one != null){
                heCar(car,one);
            }else {
                //放入跑道
                changeState(car,Constants.CAR_STATE_2);
                car.setState(Constants.CAR_STATE_2);
                listCar.add(car);
            }
        }
    }
    //是否可以合成
   private Car canHe(Car car){
        for(int i=0;i<listCar.size();i++){
            Car one = listCar.get(i);
            if(one.getLevel() == car.getLevel() && one.getState() != Constants.CAR_STATE_0 && one.getPos() != car.getPos()){
                return one;
            }
        }
        return null;
   }
   //合成
   private void heCar(Car car1,Car car2){
       if(car2.getState() != Constants.CAR_STATE_1){
           changeState(car2,Constants.CAR_STATE_1);
       }
       String s = CommonUtil.get(Constants.COMPOSE_NEW_CAR_URL + CommonUtil.buildTokenAndTime(this.token) + String.format("&pos1=%s&pos2=%s", car1.getPos(), car2.getPos()));
       JSONObject jsonObject = JSONObject.parseObject(s);
       System.out.println("heCar----:"+s);
       if(jsonObject.get("getCar")!=null){
           //移除car1和car2
           listCar.remove(car1);
           listCar.remove(car2);
           car2.setLevel(car2.getLevel() + 1);
           listCar.add(car2);
           //递归合车
           Car one = canHe(car2);
           if(one != null){
               heCar(car2,one);
           }else{
               changeState(car2,Constants.CAR_STATE_2);
               System.out.println(JSON.toJSONString(listCar));
           }
       }
   }
   //改变车的状态
    private void changeState(Car car,int state){
        String s = CommonUtil.get(Constants.CHANGE_POS_URL + CommonUtil.buildTokenAndTime(this.token) + String.format("&pos=%s&state=%s", car.getPos(), state));
        System.out.println("changeState----:"+s);
   }
}
